package com.lijiajian;

import com.alibaba.fastjson.JSON;
import com.lijiajian.base.ResultInfo;
import com.lijiajian.exceptions.AuthException;
import com.lijiajian.exceptions.NoLoginException;
import com.lijiajian.exceptions.ParamsException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.annotation.Annotation;

@Component
public class GlobalExceptionHandler implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        //处理未登录异常
        if(e instanceof NoLoginException){
            ModelAndView mv=new ModelAndView("redirect:/index");
            return mv;
        }


        //设置默认异常处理（返回视图）
        ModelAndView modelAndView = new ModelAndView("error");
        //设置异常信息
        modelAndView.addObject("code","500");
        modelAndView.addObject("msg","系统异常，请重试");

        //判断handlerMethod
        if(o instanceof HandlerMethod){
            //类型转换
            HandlerMethod handlerMethod=(HandlerMethod)o;
            Annotation annotation = handlerMethod.getMethod().getDeclaredAnnotation(ResponseBody.class);
            if(annotation ==null){
                //返回的是视图
                //判断是否是自定义异常
                if(e instanceof ParamsException){
                    modelAndView.addObject("code",((ParamsException) e).getCode());
                    modelAndView.addObject("msg",((ParamsException) e).getMsg());
                }else if(e instanceof AuthException){
                    modelAndView.addObject("code",((AuthException) e).getCode());
                    modelAndView.addObject("msg",((AuthException) e).getMsg());
                }
                return modelAndView;
            }else{
                //返回的是数据,用流写出去
                ResultInfo resultInfo=new ResultInfo();
                //默认异常处理
                resultInfo.setCode(500);
                resultInfo.setMsg("异常，请重试");
                //自定义异常处理
                if(e instanceof ParamsException){
                    resultInfo.setMsg(((ParamsException) e).getMsg());
                    resultInfo.setCode(((ParamsException) e).getCode());
                }else if(e instanceof AuthException){
                    resultInfo.setCode(((AuthException) e).getCode());
                    resultInfo.setMsg(((AuthException) e).getMsg());
                }
                //设置响应类型及编码格式
                httpServletResponse.setContentType("application/json;charset=utf-8");
                PrintWriter writer =null;
                try {
                    writer = httpServletResponse.getWriter();
                    String json= JSON.toJSONString(resultInfo);
                    writer.write(json);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                finally {
                    if(writer!=null){
                        writer.close();
                    }
                }

                return null;


            }

        }



        return modelAndView;
    }
}
